#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys


#
# 创建人   : admin
# 创建时间  : 2021/12/17
# 功能描述  : 1、..
#           2、..
#           3、..
#
def write_report(content='', f=os.getcwd() + "/report.txt"):
    print(str.strip(content), file=f)


if __name__ == '__main__':
    f_1_1 = '1.1.txt'
    f_1_2 = '1.2.txt'
    f_2 = '2.txt'
    f_4 = '4.txt'
    f_raid = 'raid.txt'
    f_check_date = 'check_date.txt'
    f_cpu = 'cpu.txt'
    f_memory = 'memory.txt'
    f_memory1 = 'memory1.txt'

    root ='/opt/check/'
    _dir =sys.argv[1]
    os.chdir(root + "/data/"+_dir)
    #_dir = os.getcwd()  
    report_file_path = root + ("/report/%s.txt" % str(_dir))
    print("当前目录:%s"%os.getcwd())
    with open(report_file_path, "w", encoding="utf-8") as report_file:
        report_file.truncate()
        write_report("数据采集时间:%s" % open(f_check_date, encoding='UTF-8').readlines()[0].replace("\n", ""), report_file)
        disks = open(f_1_1, encoding='UTF-8').readlines()
        pcie_list = open(f_1_2, encoding='UTF-8').readlines()
        disks = open(f_1_1, encoding='UTF-8').readlines()

        di = {}
        for pcie in pcie_list:
            pcie_info = pcie.split("/dev/")
            if len(pcie.replace("\n", "")) > 5 and len(pcie_info) > 1:
                di[pcie_info[1].replace("\n", "").replace(" ", "")] = pcie
        write_report("`\n======= %s ===磁盘========数量: %s" % (_dir, len(disks)), report_file)
        for disk in disks:
            part = disk.replace("\n", "").split(" ")
            write_report(("%s %s" % (di[part[0]], part[len(part) - 1])).replace("\n", ""), report_file)
        cpus = open(f_cpu, encoding='UTF-8').readlines()

        tt = []
        for line in cpus:
            if len(line) > 3:
                tt.append(line.replace("\n", "").replace("\t", ""))
        i = int(len(tt) / 2)
        write_report("`\n===== %s ===CPU========数量: %s" % (_dir, i), report_file)
        while i > 0:
            a = i * 2
            write_report("%s  %s" % (tt[a - 2].split(": ")[1], tt[a - 1].replace("\n", "")), report_file)
            i = i - 1

        ttx = []
        raides = open(f_raid, encoding='UTF-8').readlines()
        for line in raides:
            if len(line) > 3:
                ttx.append(line.replace("\n", "").replace("\t", ""))
        i = int(len(ttx) / 2)
        write_report("`\n===== %s ===Raid卡========数量: %s" % (_dir, int(len(ttx) / 2)), report_file)
        while i > 0:
            a = i * 2
            write_report(ttx[a - 1], report_file)
            i = i - 1

        eths = open(f_4, encoding='UTF-8').readlines()
        write_report("`\n===== %s ===网卡========数量: %s" % (_dir, len(eths)), report_file)
        for item in eths:
            write_report(item.split("Ethernet controller")[1].replace("\n", "").replace("\t", ""), report_file)

        memories = open(f_memory, encoding='UTF-8').readlines()

        tt1 = []
        for memory in memories:
            if len(memory) > 3:
                tt1.append(memory.replace("\n", "").replace("\t", ""))

        xx = open(f_memory1, encoding='UTF-8').readlines()
        mem_count = int(len(tt1) / 4)
        write_report("`\n===== %s ===内存========数量: %s, 总量: %s" % (_dir, mem_count, str.strip(xx[0])), report_file)
        i=mem_count
        while i > 0:
            a = i * 4
            write_report("%s  %s  %s  %s " % (tt1[a - 1],   tt1[a - 2].replace("\n", "").replace("\t", ""),  tt1[a - 3].replace("\n", "").replace("\t", ""),tt1[a - 4].replace("\n", "").replace("\t", ""),), report_file)
            i = i - 1
        lines = open("5.txt").readlines();
        
        card_info = []
        xx = False
        info = []
        for i in range(0, len(lines)):
            _str = lines[i].replace("\n", "").replace("\t", "")
            if len(_str) <= 1:
                #碰见空行了
                xx = True
            else:
                if not xx:
                    info.append(_str)
                else:
                    info.append(lines[i - 1])
                    card_info.append(info)
                    info = []
                    xx = False
        pass
        write_report("`\n===== %s ===各种板卡信息(排除了可能是空卡槽的数据)========数量: %s (数量统计包含空卡槽)" % (_dir, len(card_info)), report_file)
        for item in card_info:
            line_str ="####".join(item)
            if len(line_str) >200:
                write_report(line_str, report_file)

        os.chdir(os.path.abspath(os.path.join(os.getcwd(), "..")))
    pass
pass
